package com.tianyi.wz.walkinggame.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.tianyi.wz.walkinggame.Constant;
import com.tianyi.wz.walkinggame.common.ActivityManager;
import com.tianyi.wz.walkinggame.common.ToastCommon;
import com.tianyi.wz.walkinggame.R;

import org.apache.commons.lang3.StringUtils;

public class BaseActivity extends Activity {

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        ActivityManager.getInstance().addActivity(this);
        overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void finish() {
        super.finish();
        //BaseApplication.getInstance().removeActivity(this);
        overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
    }

    /*-------------------- 退出代码Start --------------------*/

    /**
     * 两次返回退出状态值
     */
    private boolean exitStatus = false;

    /**
     * 退出应用
     */
    public void exit(){
        this.exit("");
    }

    /**
     * 退出应用
     * @param exitMsg
     */
    public void exit(String exitMsg) {
        if(exitStatus){
            ActivityManager.getInstance().exitApp();
        }else{
            ToastCommon.showToast(this, StringUtils.isNotEmpty(exitMsg)?exitMsg: Constant.MESSAGE.EXIT_APP);
            exitStatus = true;
            handler.sendEmptyMessageDelayed(0,2000);
        }
    }

    /**
     * 按返回后两秒复位
     */
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            exitStatus = false;
        }
    };

    /*-------------------- 退出代码 End --------------------*/

    protected String getRunningActivityName(){
        String contextString = this.toString();
        return contextString.substring(contextString.lastIndexOf(".")+1, contextString.indexOf("@"));
    }

}
